package com.lxm.enigma.algorithm;

import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang3.StringUtils;

/**
 * @Author: Lys
 * @Date 2022/5/31
 * @Describe
 **/
public class Md5Algorithm implements Algorithm {

    @Override
    public String encipher(Object content, String key) throws Exception {
        String sorted = "";
        if (content instanceof String) {
            sorted = content.toString();
        } else {
            var map = Md5Utils.obj2Map(content, false);
            var strArr = Md5Utils.map2Arr(map);
            sorted = Md5Utils.sort(strArr);
        }
        return StringUtils.isNotBlank(key) ? DigestUtils.md5Hex(sorted.concat(key)) : DigestUtils.md5Hex(sorted);
    }

    @Override
    public String decipher(Object content, String key) throws Exception {
        throw new RuntimeException("md5 cannot be deciphered");
    }
}
